import java.utili.ArrayList;
import java.util.Scanner;

public class StudentManagement {
    static class Student {
        String name;
        String id;
        String course;
        double score;
    }

    static ArrayList<Student> students = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n欢迎使用学生成绩管理系统");
            System.out.println("1. 记录学生成绩\n2. 查询学生成绩\n3. 统计课程成绩\n4. 退出系统");
            System.out.print("请输入选项序号：");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    queryStudent();
                    break;
                case "3":
                    showStats();
                    break;
                case "4":
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("错误：无效选项！");
            }
        }
    }

    static void addStudent() {
         System.out.println("\n—— 记录学生成绩 ——");
        Student s = new Student();
        System.out.print("请输入学生姓名：");
        s.name = scanner.nextLine();
        System.out.print("请输入学生学号：");
        s.id = scanner.nextLine();
        System.out.print("请输入课程名称：");
        s.course = scanner.nextLine();
        System.out.print("请输入成绩（0-100）：");
        s.score = Double.parseDouble(scanner.nextLine());
        if (s.score < 0 || s.score > 100) {
            System.out.println("错误：成绩必须在 0-100 之间！");
            return;
        }
        students.add(s);
      System.out.println("成绩已成功记录！");
    }

    static void queryStudent() {
        System.out.println("\n—— 查询学生成绩 ——");
        System.out.println("1. 按学生姓名查询\n2. 按学生学号查询\n3. 按课程名称查询");
        System.out.print("请输入选项序号：");
        String choice = scanner.nextLine();
        System.out.print("请输入查询关键词：");
        String keyword = scanner.nextLine();
        boolean found = false;
        for (Student s : students) {
            if ((choice.equals("1") && s.name.equals(keyword)) ||
                    (choice.equals("2") && s.id.equals(keyword)) ||
                    (choice.equals("3") && s.course.equals(keyword))) {
                System.out.printf("姓名：%s，学号：%s，课程：%s，成绩：%.1f\n",
                        s.name, s.id, s.course, s.score);
                found = true;
            }
        }
        if (!found) System.out.println("未找到匹配记录！");
    }

    static void showStats() {
        System.out.println("\n—— 统计课程成绩 ——");
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        double sum = 0, max = 0, min = 100;
        int count = 0;
        for (Student s : students) {
            if (s.course.equals(course)) {
                sum += s.score;
                max = Math.max(max, s.score);
                min = Math.min(min, s.score);
                count++;
            }
        }
        if (count == 0) {
            System.out.println("课程 '" + course + "' 无记录！");
        } else {
            System.out.printf("平均分：%.1f，最高分：%.1f，最低分：%.1f\n", sum / count, max, min);
        }
    }
}



